/*
 * Copyright (c) 2008 Sergey Rudchenko <sergey.rudchenko@gmail.com>
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "sprite.h"

#include <stdlib.h>
#include <string.h>

void sprite_sync(struct sprite* sprite)
{
    sprite->x = sprite->new_x;
    sprite->y = sprite->new_y;
    sprite->_rect.x = sprite->x;
    sprite->_rect.y = sprite->y;
    sprite->_rect.w = sprite->image->w;
    sprite->_rect.h = sprite->image->h;
    sprite->dirty = 0;
}

struct sprite* new_sprite(SDL_Surface* image)
{
    struct sprite* obj = malloc(sizeof(struct sprite));
    obj->image = image;
    obj->new_x = 0;
    obj->new_y = 0;
    sprite_sync(obj);
    return obj;
}

void sprite_move(struct sprite* sprite, double dx, double dy)
{
    sprite->new_x = sprite->x + dx;
    sprite->new_y = sprite->y + dy;
    sprite->dirty = 1;
}

void sprite_blit(struct sprite* sprite, SDL_Surface* screen, SDL_Surface* bg)
{
    if (sprite->dirty)
    {
        SDL_BlitSurface(bg, &sprite->_rect, screen, &sprite->_rect);
        sprite_sync(sprite);
    }

    SDL_BlitSurface(sprite->image, NULL, screen, &sprite->_rect);
}

